#include "dlom2_sensor.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "app_task.h"
#include "user_mb_app.h"
#include "usart.h"
#include "tim.h"
#include "data_convert.h"
#include "modbus_master.h"
#include "trans_recieve_buff_control.h"

dlo_m2_sensor_mdbus_t dlom2_sensor_value = { 0 };
char pbuf[20] = {0};
void py_f2s4printf(char *stra, float x, uint8_t flen) {
	uint32_t base;
	int64_t dn;
	char mc[32];

	base = pow(10, flen);
	dn = x * base;
	sprintf(stra, "%d.", (int) (dn / base));
	dn = abs(dn);
	if (dn % base == 0) {
		for (uint8_t j = 1; j <= flen; j++) {
			stra = strcat(stra, "0");
		}
		return;
	} else {
		if (flen == 1) {
			sprintf(mc, "%d", (int) (dn % base));
			stra = strcat(stra, mc);
			return;
		}

		for (uint8_t j = 1; j < flen; j++) {
			if ((dn % base) < pow(10, j)) {
				for (uint8_t k = 1; k <= (flen - j); k++) {
					stra = strcat(stra, "0");
				}
				sprintf(mc, "%d", (int) (dn % base));
				stra = strcat(stra, mc);
				return;
			}
		}
		sprintf(mc, "%d", (int) (dn % base));
		stra = strcat(stra, mc);
		return;
	}
}



void dlom2_init(uint16_t slave_id) {
	dlom2_sensor_value.slave_id = slave_id;
	dlom2_sensor_value.calib_std_den_state = 0;
	dlom2_sensor_value.calib_std_vis_state = 0;
	ModbusMaster_begin();
	HAL_UART_Receive_IT(&huart3, rxdata, 1);
}
void dlom2_calibratio() {

	if (dlom2_sensor_value.maintenance_state == 1) {

		dlom2_set_maintenance();
		dlom2_sensor_value.maintenance_state = 0;
	}
	if (dlom2_sensor_value.calib_std_den_state == 1) {

		dlom2_set_calibration_density_from_slave();
		dlom2_sensor_value.calib_std_den_state = 0;
	}
	if (dlom2_sensor_value.calib_std_vis_state == 1) {

		dlom2_set_calibration_viscosity_from_slave();
		dlom2_sensor_value.calib_std_vis_state = 0;
	}


}

float dlom2_get_density() {

	uint16_t result[2] = { 0 };
	uint8_t ret = 0;
	ret = ModbusMaster_readHoldingRegisters(dlom2_sensor_value.slave_id, Density_Offset, 2);
	if (ret == 0x00) {
		result[0] = ModbusMaster_getResponseBuffer(0x00);
		result[1] = ModbusMaster_getResponseBuffer(0x01);

		Slave_station.usSRegHoldBuf[Density_Reg] = result[0];
		Slave_station.usSRegHoldBuf[Density_Reg + 1] = result[1];
	}
	dlom2_sensor_value.densityValue = uint16ToFloat(result);
#if DLOM2_DEBUG
	memset(pbuf,0,20);
	py_f2s4printf(pbuf, dlom2_sensor_value.densityValue, 3);
	printf("densityValue : %s \r\n", pbuf);
#endif
	return dlom2_sensor_value.densityValue;
}
float dlom2_get_dynviscosity() {
	uint16_t result[2] = { 0 };
	uint8_t ret = 0;
	ret = ModbusMaster_readHoldingRegisters(dlom2_sensor_value.slave_id, DynamicViscosity_Offset,
			2);
	if (ret == 0x00) {
		result[0] = ModbusMaster_getResponseBuffer(0x00);
		result[1] = ModbusMaster_getResponseBuffer(0x01);

		Slave_station.usSRegHoldBuf[DynViscosity_Reg] = result[0];
		Slave_station.usSRegHoldBuf[DynViscosity_Reg + 1] = result[1];

	}
	dlom2_sensor_value.dynamicViscosityValue = uint16ToFloat(result);
#if DLOM2_DEBUG
	memset(pbuf,0,20);
	py_f2s4printf(pbuf, dlom2_sensor_value.dynamicViscosityValue, 3);
	printf("dynamicViscosityValue : %s \r\n", pbuf);
#endif
	return dlom2_sensor_value.dynamicViscosityValue;
}
float dlom2_get_kinviscosity() {
	uint16_t result[2] = { 0 };
	uint8_t ret = 0;
	ret = ModbusMaster_readHoldingRegisters(dlom2_sensor_value.slave_id,
			KinematicViscosity_Offset, 2);
	if (ret == 0x00) {
		result[0] = ModbusMaster_getResponseBuffer(0x00);
		result[1] = ModbusMaster_getResponseBuffer(0x01);

		Slave_station.usSRegHoldBuf[KinViscosity_Reg] = result[0];
		Slave_station.usSRegHoldBuf[KinViscosity_Reg + 1] = result[1];

	}
	dlom2_sensor_value.kinematicViscosityValue = uint16ToFloat(result);

#if DLOM2_DEBUG
	memset(pbuf,0,20);
	py_f2s4printf(pbuf, dlom2_sensor_value.kinematicViscosityValue, 3);
	printf("kinematicViscosityValue : %s \r\n", pbuf);
#endif
	return dlom2_sensor_value.kinematicViscosityValue;
}
float dlom2_get_temperature() {
	uint16_t result[2] = { 0 };
	uint8_t ret = 0;
	ret = ModbusMaster_readHoldingRegisters(dlom2_sensor_value.slave_id, Temperature_Offset, 2);
	if (ret == 0x00) {
		result[0] = ModbusMaster_getResponseBuffer(0x00);
		result[1] = ModbusMaster_getResponseBuffer(0x01);

		Slave_station.usSRegHoldBuf[Temperature_Reg] = result[0];
		Slave_station.usSRegHoldBuf[Temperature_Reg + 1] = result[1];

	}
	dlom2_sensor_value.temperatureValue = uint16ToFloat(result);
#if DLOM2_DEBUG
	memset(pbuf,0,20);
	py_f2s4printf(pbuf, dlom2_sensor_value.temperatureValue, 3);
	printf("temperature : %s \r\n",p buf);
#endif
	return dlom2_sensor_value.temperatureValue;
}
void dlom2_set_maintenance() {

	ModbusMaster_writeSingleRegister(dlom2_sensor_value.slave_id, EnterAccessCodeOffset,
			EnterAccessCode);
}

void dlom2_set_calibration_density_from_slave() {

	ModbusMaster_setTransmitBuffer(0,
			Slave_station.usSRegHoldBuf[StdDensity_Reg]);
	ModbusMaster_setTransmitBuffer(1,
			Slave_station.usSRegHoldBuf[StdDensity_Reg + 1]);
	ModbusMaster_writeMultipleRegisters(dlom2_sensor_value.slave_id, DensitySinglePoint_Offset,
			2);
}
void dlom2_set_calibration_viscosity_from_slave() {

	ModbusMaster_setTransmitBuffer(0,
			Slave_station.usSRegHoldBuf[StdViscosity_Reg]);
	ModbusMaster_setTransmitBuffer(1,
			Slave_station.usSRegHoldBuf[StdViscosity_Reg + 1]);
	ModbusMaster_writeMultipleRegisters(dlom2_sensor_value.slave_id, ViscositySinglePoint_Offset,
			2);
}
void modbus_test() {

	uint16_t result[2] = { 0 };
	uint8_t ret = 0;
	float densityValue = 0;
	ret = ModbusMaster_readHoldingRegisters(dlom2_sensor_value.slave_id, Density_Offset, 2);
	if (ret == 0x00) {
		result[0] = ModbusMaster_getResponseBuffer(0x00);
		result[1] = ModbusMaster_getResponseBuffer(0x01);
		densityValue = uint16ToFloat(result);
		memset(pbuf,0,20);
		py_f2s4printf(pbuf, densityValue, 3);
		printf("read : %s \r\n", pbuf);
	}

	ModbusMaster_writeSingleRegister(dlom2_sensor_value.slave_id, 2014, EnterAccessCode);

	uint16_t sBuf[2] = { 0 };
	floatToUint16(densityValue, sBuf);
	printf("high : %d \r\n", sBuf[0]);
	printf("low : %d \r\n", sBuf[1]);
	ModbusMaster_setTransmitBuffer(0, sBuf[0]);
	ModbusMaster_setTransmitBuffer(1, sBuf[1]);
	ModbusMaster_writeMultipleRegisters(dlom2_sensor_value.slave_id, 2018, 2);

}
/// @brief 读取一次值
void dlom2_read_value_poll() {

	dlom2_get_density();
	dlom2_get_dynviscosity();
	dlom2_get_kinviscosity();
	dlom2_get_temperature();
	dlom2_calibratio();

}
